package com.medical.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.system.mapper.SysUserMapper;
import com.medical.system.model.SysUser;
import com.medical.system.service.SysMenuService;
import com.medical.system.service.SysUserService;
import com.medical.system.vo.RouterVo;
import com.medical.system.vo.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService sysMenuService;
    //    分页查询
    @Override
    public IPage<SysUser> selectPage(IPage<SysUser> iPage, SysUserQueryVo sysUserQueryVo) {
        return this.baseMapper.selectPage(iPage,sysUserQueryVo);
    }

    //    更新用户状态
    @Override
    public void updateStatus(Long id, Integer status) {
        //1.查询出当前用户
        SysUser sysUser = this.baseMapper.selectById(id);
        if (status instanceof Integer) {
            sysUser.setStatus(status.intValue());
        } else if (status instanceof Number) { // 如果可能的话，处理其他数字类型
            sysUser.setStatus(((Number)status).intValue());
        }
        //3.更新
        this.baseMapper.updateById(sysUser);
    }


    //    根据用户名获取用户信息
    @Override
    public SysUser getUserInfoUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq("username",username);
        SysUser sysUser = this.baseMapper.selectOne(queryWrapper);
        return sysUser;
    }

    //    获取用户信息
    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String, Object> map = new HashMap<>();

        //a.用户的基本信息
        SysUser sysUser = this.getUserInfoUserName(username);

        //当前权限控制使用不到，我们暂时忽略
        map.put("name", sysUser.getUsername());
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("roles",  "[admin]");

        //b.根据用户id获取菜单权限值
        List<RouterVo> routerVoList = sysMenuService.findUserMenuList(sysUser.getId());

        //c.根据用户id获取用户按钮权限
        List<String> permsList = sysMenuService.findUserPermsList(sysUser.getId());

        map.put("buttons", permsList);
        map.put("routers", routerVoList);
        return map;
    }
}
